package com.yx.system.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.utils.Query;
import com.yx.modules.api.dao.*;
import com.yx.modules.api.entity.*;
import com.yx.system.dao.TdBannerMapper;
import com.yx.system.dao.TdBookMapper;
import com.yx.system.dao.TdCollectionMapper;
import com.yx.system.dao.TdSectionMapper;
import com.yx.system.entity.*;
import com.yx.system.service.TdBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

@Service("tdBookService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TdBookServiceImpl implements TdBookService {

    @Autowired
    private TdBookMapper tdBookMapper;

    @Autowired
    //图书评论
    private TbBookCommentDao tbBookCommentDao;

    @Autowired
    //图书评论点赞记录
    private TbCommentDianzanRecordDao tbCommentDianzanRecordDao;

    @Autowired
    //图书点赞记录
    private TbBookRecommendRecordDao tbBookRecommendRecordDao;

    @Autowired
    //图书点击记录
    private TbClickDao tbClickDao;

    @Autowired
    //图书打分记录
    private TbGradeRecordDao tbGradeRecordDao;

    @Autowired
    //图书打赏记录
    private TbRewardRecordDao tbRewardRecordDao;

    @Autowired
    //图书收藏记录
    private TdCollectionMapper tdCollectionMapper;

    @Autowired
    private TdBannerMapper tdBannerMapper;

    @Autowired
    private TdSectionMapper tdSectionMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return tdBookMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TdBook record) {
        return tdBookMapper.insert(record);
    }

    @Override
    public int insertSelective(TdBook record) {
        return tdBookMapper.insertSelective(record);
    }

    @Override
    public TdBook selectByPrimaryKey(Integer id) {
        return tdBookMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TdBook record) {
        return tdBookMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TdBook record) {
        return tdBookMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<TdBook> queryByName(String bookName) {
        return tdBookMapper.queryByName(bookName);
    }

    @Override
    public List<TdBook> queryList(Map<String, Object> map) {
        return tdBookMapper.queryList(map);
    }

    @Override
    public Page<TdBook> queryPageList(Page<TdBook> page, Map<String, Object> map) {
        page.setRecords(tdBookMapper.queryPageList(page, map));
        return page;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return tdBookMapper.queryTotal(map);
    }

    @Override
    public int queryTotalByClass(int classid) {
        return tdBookMapper.queryTotalByClass(classid);
    }

    @Override
    public List<TdBook> queryByTypeId(int categoryId) {
        return tdBookMapper.queryByTypeId(categoryId);
    }

    @Override
    public List<TdBook> queryByClassId(int classificationId) {
        return tdBookMapper.queryByClassId(classificationId);
    }

    @Override
    public TdBook queryByUuid(String uuid) {
        return tdBookMapper.queryByUuid(uuid);
    }

    @Override
    public List<TdBook> queryAll() {
        return tdBookMapper.queryAll();
    }

    @Override
    public List<TdBook> queryByDay() {
        return tdBookMapper.queryByDay();
    }

    @Override
    public int queryTotalByConditionId(Integer id) {
        return tdBookMapper.queryTotalBycategoryid(id);
    }

    @Override
    public List<TdBook> findByAuthorCode(String authorId) {
        return tdBookMapper.findByAuthorCode(authorId);
    }

    @Override
    public List<TdBook> findByRand() {
        return tdBookMapper.findByRand();
    }

    @Override
    public TdBook findOneByRand() {
        return tdBookMapper.findOneByRand();
    }

    @Override
    public void delete(Integer[] ids) {
        for (int i = 0 ; i<ids.length;i++) {
            int id = ids[i];
            TdBook tdBook = tdBookMapper.selectByPrimaryKey(id);
            tdBook.setIsdelete(true);
            tdBookMapper.updateByPrimaryKey(tdBook);
            //删除图书评论
            List<TbBookComment> tbBookComments = tbBookCommentDao.findByBookid(tdBook.getId());
            if (tbBookComments.size()!=0){
                for (TbBookComment tbBookComment:tbBookComments){
                    if (tbBookComment.getType()==0){
                        //找到图书评论对应的点赞记录并删除
                        List<TbCommentDianzanRecord> tbCommentDianzanRecords = tbCommentDianzanRecordDao.findByCommentid(tbBookComment.getId());
                        if (tbCommentDianzanRecords.size()!=0){
                            for (TbCommentDianzanRecord tbCommentDianzanRecord:tbCommentDianzanRecords){
                                tbCommentDianzanRecordDao.delete(tbCommentDianzanRecord.getId());
                            }
                        }
                    }
                    tbBookCommentDao.delete(tbBookComment.getId());
                }
            }
            //删除图书点赞记录
            List<TbBookRecommendRecord> tbBookRecommendRecords = tbBookRecommendRecordDao.findByBookid(tdBook.getId());
            if (tbBookRecommendRecords.size()!=0){
                for (TbBookRecommendRecord tbBookRecommendRecord:tbBookRecommendRecords){
                    tbBookRecommendRecordDao.delete(tbBookRecommendRecord.getId());
                }
            }
            //删除图书点击记录
            List<TbClick> tbClicks = tbClickDao.findByBookid(tdBook.getId());
            if (tbClicks.size()!=0){
                for (TbClick tbClick:tbClicks){
                    tbClickDao.delete(tbClick.getId());
                }
            }
            //删除图书打分记录
            List<TbGradeRecord> tbGradeRecords = tbGradeRecordDao.findByBookid(tdBook.getId());
            if (tbGradeRecords.size()!=0){
                for (TbGradeRecord tbGradeRecord:tbGradeRecords){
                    tbGradeRecordDao.delete(tbGradeRecord.getId());
                }
            }
            //删除图书打赏记录
            List<TbRewardRecord> tbRewardRecords = tbRewardRecordDao.findByBookid(tdBook.getId());
            if(tbRewardRecords.size()!=0){
                for (TbRewardRecord tbRewardRecord:tbRewardRecords){
                    tbRewardRecordDao.delete(tbRewardRecord.getId());
                }
            }
            //删除图书收藏记录
            List<TdCollection> tdCollections = tdCollectionMapper.findByBookid(tdBook.getId());
            if (tdCollections.size()!=0){
                for (TdCollection tdCollection:tdCollections){
                    tdCollectionMapper.deleteByPrimaryKey(tdCollection.getId());
                }
            }
            //删除banner图片记录
            List<TdBanner> tdBanners = tdBannerMapper.findByBookid(tdBook.getId());
            if (tdBanners.size()!=0){
                for (TdBanner tdBanner:tdBanners){
                    tdBannerMapper.deleteByPrimaryKey(tdBanner.getId());
                }
            }

        }
    }

    @Override
    public void pricing(TdBook tdBook,List<TdSection> tdSections, TbKwordPrice tbKwordPrice, Integer start) {
        if (tdSections.size()!=0){
            //首先将之前章节价格归零
            System.out.println("================================输出免费===================================");
            for (int i = 0; i < start; i++) {
                TdSection section = tdSections.get(i);
                section.setTotalfee(0);
                section.setSectionType(0);
                //更新章节状态
                tdSectionMapper.updateByPrimaryKey(section);
            }
            System.out.println("================================输出收费===================================");
            //循环改变章节价格
            for (int i = start-1;i<tdSections.size();i++){
                TdSection section = tdSections.get(i);
                //取得章节字数
                Double font = Double.valueOf(section.getFontsize());
                //四舍五入保留两位小数
                BigDecimal bg = new BigDecimal(font/1000);
                Double fontSize = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                //千字收费*字数，计算价格
                section.setTotalfee((int) (tbKwordPrice.getPrice()*fontSize));
                section.setSectionType(1);
                //更新章节状态
                tdSectionMapper.updateByPrimaryKey(section);
            }
        }
        //改变图书状态
        tdBook.setIspay(true);
        tdBookMapper.updateByPrimaryKey(tdBook);
    }

    @Override
    public List<TdBook> findAllOrderBySubscibe() {
        return tdBookMapper.findAllOrderBySubscibe();
    }

    @Override
    public List<TdBook> queryGameList(Map<String, Object> map) {
        return tdBookMapper.queryGameList(map);
    }

    @Override
    public int queryGameTotal(Map<String, Object> map) {
        return tdBookMapper.queryGameTotal(map);
    }

    @Override
    public List<TdBook> queryGameCrunchiesList(Map<String, Object> map) {
        return tdBookMapper.queryGameCrunchiesList(map);
    }

    @Override
    public int queryGameCrunchiesTotal(Map<String, Object> map) {
        return tdBookMapper.queryGameCrunchiesTotal(map);
    }

    public static void main(String[] args){
        BigDecimal bg = new BigDecimal(233.253);
        Double fontSize = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(fontSize);
    }

}
